package com.crm.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.crm.domain.BusinessOpportunity;
import com.crm.crm.domain.Contract;
import com.crm.crm.domain.Course;
import com.crm.crm.domain.dto.CourseLIstDto;
import com.crm.crm.mapper.CourseMapper;
import com.crm.crm.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class CourseServicelmpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    //新增课程
    @Override
    public Integer add(Course course) {
        log.info("新增课程{}", course);
//        设置创建时间和更新时间
//        todo 修改人和新建人未设置，需要等登录校验之后新增
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        return courseMapper.insert(course);
    }

    //课程分页查询
    @Override
    public Page<Course> list(CourseLIstDto dto, Page<Course> page) {
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();

        // 根据起始时间查询。判断是否有内容，没有则查询所有
//        wrapper.ge(StringUtils.isNotEmpty(dto.getBeginCreateTime()), Course::getCreateTime, dto.getBeginCreateTime());
//        wrapper.ge(StringUtils.isNotEmpty(dto.getBeginCreateTime()), Course::getCreateTime, dto.getBeginCreateTime());
        if (StringUtils.isNotEmpty(dto.getBeginCreateTime())) {
            wrapper.ge(Course::getCreateTime, dto.getBeginCreateTime());
        }
        if (StringUtils.isNotEmpty(dto.getEndCreateTime())) {
            wrapper.le(Course::getCreateTime, dto.getEndCreateTime()+ " 23:59:59");
        }
//        根据科目查询
        wrapper.eq(StringUtils.isNotEmpty(dto.getSubject()), Course::getSubject, dto.getSubject());
//        根据课程名称模糊查询
        wrapper.like(StringUtils.isNotEmpty(dto.getName()), Course::getName, dto.getName());
//        根据课程编号模糊查询
        wrapper.like(StringUtils.isNotEmpty(dto.getCode()), Course::getID, dto.getCode());

        return courseMapper.selectPage(page, wrapper);
    }

    //删除课程
    @Override
    public Integer deleteByIds(String[] ids) {
        log.info("删除课程参数：{}", ids);
        int i = courseMapper.deleteByIds(Arrays.asList(ids));

        return i;
    }

    //根据ID查询课程
    @Override
    public Course getById(Integer id) {
        return courseMapper.selectById(id);
    }

    //修改课程
    @Override
    public Integer updateById(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        return courseMapper.updateById(course);
    }

    //    根据学科ID查询有这个学科的课程
    @Override
    public List<Course> listSelect(Integer subject) {
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
//        判断subject是否有内容
        if (subject != null) {
            wrapper.eq(Course::getSubject, subject);
        }
        return courseMapper.selectList(wrapper);

    }
}
